/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.dhecp.core.layui;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author DH-Xxj
 * @version 2019年9月9日
 */
@Component
public class TreeSelectDataUtil {
	
	/**
	 * 递归处理树的各级子节点  children
	 * @param treeSelectDataModelList
	 * @return treeSelectDataModelListReturn
	 * @author DH-Xxj
	 */
	
	public List<TreeSelectDataModel> dealTreeSelectDataModel(List<TreeSelectDataModel> treeSelectDataModelList, int maxlevels){
		int thislevels = maxlevels - 1;
		if (thislevels < 1){
			thislevels = 10;
		}
		List<TreeSelectDataModel> treeSelectDataModelListReturn = new ArrayList<TreeSelectDataModel>();
		if (thislevels > 0){
			for (TreeSelectDataModel treeSelectDataModel:treeSelectDataModelList){
				if (thislevels == treeSelectDataModel.getLevels()){
					List<TreeSelectDataModel> treeSelectDataModelListChildren = new ArrayList<TreeSelectDataModel>();
					for (TreeSelectDataModel treeSelectDataModelchildren:treeSelectDataModelList){
						if (treeSelectDataModel.getId().equals(treeSelectDataModelchildren.getParentId())){
							treeSelectDataModelListChildren.add(treeSelectDataModelchildren);
						}
					}
					treeSelectDataModel.setChildren(treeSelectDataModelListChildren);
				}
			}
			if (thislevels > 1){//递归
				this.dealTreeSelectDataModel(treeSelectDataModelList, thislevels);
			}
		}

		for (TreeSelectDataModel treeSelectDataModel:treeSelectDataModelList){
			if (1 == treeSelectDataModel.getLevels()){
				treeSelectDataModelListReturn.add(treeSelectDataModel);
			}
			
		}
		return treeSelectDataModelListReturn;
		
	}

}
